/**
 * 自定义函数
 * */


//获取工时
function getOne(){
    var uinfo = getUserInfo();
    var formData = {
        id: id, depart_id: uinfo.depart_id, uid: uinfo.uid,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/one',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            console.log(res.data);
            oneHtml(res.data);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}
function oneHtml(e){
    var d = e.data;
    var whList = e.whList;
    var cntList = e.cntList;
    var hour = e.hour;
    var h, i;
    //工时项
    var piddiv = $('#pid_show');
    if (config_title != '') {
        piddiv.html(config_title + '<input type="hidden" id="config_whid" value="' + config_whid + '"/>');
    } else {
        h = '';
        h += '<select class="input-text" id="config_whid" onchange="getCntList();">';
        if (config_whid == 0) {
            h += '<option value="0" selected>';
        } else {
            h += '<option value="0">';
        }
        h += '-选择工时项-';
        h += '</option>';
        for (i = 0; i < whList.length; i ++) {
            if (d.config_whid == whList[i].id) {
                h += '<option value="' + whList[i].id + '" selected>';
            } else {
                h += '<option value="' + whList[i].id + '">';
            }
            h += whList[i].title;
            h += '</option>';
        }
        h += '</select>';
        piddiv.html(h);
    }
    //内容
    h = '';
    if (d.cnt_id == 0) {
        h += '<option value="" selected>';
    } else {
        h += '<option value="0">';
    }
    h += '-选择-';
    h += '</option>';
    for (i = 0; i < cntList.length; i ++) {
        if (d.cnt_id == cntList[i].id) {
            h += '<option value="' + cntList[i].id + '" selected>';
        } else {
            h += '<option value="' + cntList[i].id + '">';
        }
        h += cntList[i].title;
        h += '</option>';
    }
    $('#cnt_id').html(h);
    //时间
    h = '';
    if (d.starthour == '') {
        h += '<option value="" selected>';
    } else {
        h += '<option value="">';
    }
    h += '-选择时间-';
    h += '</option>';
    for (i = 0; i < hour.length; i ++) {
        if (d.starthour == hour[i]) {
            h += '<option value="' + hour[i] + '" selected>';
        } else {
            h += '<option value="' + hour[i] + '">';
        }
        h += hour[i];
        h += '</option>';
    }
    $('#starthour').html(h);
    h = '';
    if (d.endhour == '') {
        h += '<option value="" selected>';
    } else {
        h += '<option value="">';
    }
    h += '-选择时间-';
    h += '</option>';
    for (i = 0; i < hour.length; i ++) {
        if (d.endhour == hour[i]) {
            h += '<option value="' + hour[i] + '" selected>';
        } else {
            h += '<option value="' + hour[i] + '">';
        }
        h += hour[i];
        h += '</option>';
    }
    $('#endhour').html(h);
    //时长
    $('#delay')[0].value = d.delay_name;
    if (config_title != '') {
        getCntList();
    }
}


//获取内容
function getCntList(){
    var formData = {
        wh_id: $('#config_whid').val(),
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/cnt-get',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            var cntList = res.data;
            var h = '', i;
            if (cnt_id == 0) {
                h += '<option value="0" selected>';
            } else {
                h += '<option value="0">';
            }
            h += '-选择内容-';
            h += '</option>';
            for (i = 0; i < cntList.length; i ++) {
                if (cnt_id == cntList[i].id) {
                    h += '<option value="' + cntList[i].id + '" selected>';
                } else {
                    h += '<option value="' + cntList[i].id + '">';
                }
                h += cntList[i].title;
                h += '</option>';
            }
            $('#cnt_id').html(h);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//获取工时
function delayByTime(){
    var start = $('#starthour').val();
    var end = $('#endhour').val();
    if (start == '' || end == '') {
        return;
    }
    var formData = {
        start: start, end: end,
    };
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/delay-get',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            $('#delay')[0].value = res.data.str;
        },
        error: function () {
            console.log('网络错误');
        }
    });
}


//修改
function modify(){
    var uinfo = getUserInfo();
    var config_whid = $('#config_whid').val();
    var cnt_id = $('#cnt_id').val();
    var starthour = $('#starthour').val();
    var endhour = $('#endhour').val();
    var formData = {
        uid: uinfo.uid, role_id: uinfo.role_id, depart_id: uinfo.depart_id,
        id: id, config_whid: config_whid, wh_id: wh_id, cnt_id: cnt_id,
        starthour: starthour, endhour: endhour,
    };
    //console.log(formData);
    $.ajax({
        type: apiMethod,
        url: apiDomain + 'whitem/modify',
        data: formData,
        success: function (res) {
            console.log(res);
            if (res.code < 0) {
                msgPop({msg: res.msg});
                return
            }
            msgPop({msg: '成功'});
            setTimeout(function(){
                //window.top.location.reload();
                window.location.href = 'whyw-edit.html?id=' + wh_id;
            }, 500);
        },
        error: function () {
            console.log('网络错误');
        }
    });
}